home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH8 / SRC / SIERP.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-03-07  |  5.9 KB  |  192 lines

  1. VERSION 4.00
  2. Begin VB.Form SierpForm 
  3.    Caption         =   "Sierpinski"
  4.    ClientHeight    =   4335
  5.    ClientLeft      =   2280
  6.    ClientTop       =   1185
  7.    ClientWidth     =   5070
  8.    Height          =   5025
  9.    Left            =   2220
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   289
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   338
  14.    Top             =   555
  15.    Width           =   5190
  16.    Begin VB.TextBox LevelText 
  17.       Height          =   285
  18.       Left            =   600
  19.       MaxLength       =   3
  20.       TabIndex        =   0
  21.       Text            =   "5"
  22.       Top             =   0
  23.       Width           =   375
  24.    End
  25.    Begin VB.PictureBox Canvas 
  26.       AutoRedraw      =   -1  'True
  27.       Height          =   4335
  28.       Left            =   1080
  29.       ScaleHeight     =   285
  30.       ScaleMode       =   3  'Pixel
  31.       ScaleWidth      =   261
  32.       TabIndex        =   3
  33.       Top             =   0
  34.       Width           =   3975
  35.    End
  36.    Begin VB.CommandButton CmdGo 
  37.       Caption         =   "Go"
  38.       Default         =   -1  'True
  39.       Height          =   495
  40.       Left            =   120
  41.       TabIndex        =   1
  42.       Top             =   480
  43.       Width           =   735
  44.    End
  45.    Begin VB.Label Label1 
  46.       Caption         =   "Level"
  47.       Height          =   255
  48.       Index           =   0
  49.       Left            =   0
  50.       TabIndex        =   2
  51.       Top             =   0
  52.       Width           =   495
  53.    End
  54.    Begin VB.Menu mnuFile 
  55.       Caption         =   "&File"
  56.       Begin VB.Menu mnuFileExit 
  57.          Caption         =   "E&xit"
  58.       End
  59.    End
  60. Attribute VB_Name = "SierpForm"
  61. Attribute VB_Creatable = False
  62. Attribute VB_Exposed = False
  63. Option Explicit
  64. Dim TheLevel As Integer
  65. Dim StartLength As Single
  66. ' Maximum space the curve can take up.
  67. Dim TotalLength As Integer
  68. Dim StartX As Integer
  69. Dim StartY As Integer
  70. ' ************************************************
  71. ' Draw the complete Sierpinski curve.
  72. ' ************************************************
  73. Sub Sierpinski(level As Integer, Dist As Single)
  74.     SierpB level, Dist
  75.     Canvas.Line -Step(Dist, Dist)
  76.     SierpC level, Dist
  77.     Canvas.Line -Step(Dist, -Dist)
  78.     SierpD level, Dist
  79.     Canvas.Line -Step(-Dist, -Dist)
  80.     SierpA level, Dist
  81.     Canvas.Line -Step(-Dist, Dist)
  82. End Sub
  83. ' ************************************************
  84. ' Draw a type B sierpinski sub-curve.
  85. ' ************************************************
  86. Sub SierpB(level As Integer, Dist As Single)
  87.     If level = 1 Then
  88.         Canvas.Line -Step(Dist, Dist)
  89.         Canvas.Line -Step(0, Dist)
  90.         Canvas.Line -Step(-Dist, Dist)
  91.     Else
  92.         SierpB level - 1, Dist
  93.         Canvas.Line -Step(Dist, Dist)
  94.         SierpC level - 1, Dist
  95.         Canvas.Line -Step(0, Dist)
  96.         SierpA level - 1, Dist
  97.         Canvas.Line -Step(-Dist, Dist)
  98.         SierpB level - 1, Dist
  99.     End If
  100. End Sub
  101. ' ************************************************
  102. ' Draw a type C sierpinski sub-curve.
  103. ' ************************************************
  104. Sub SierpC(level As Integer, Dist As Single)
  105.     If level = 1 Then
  106.         Canvas.Line -Step(Dist, -Dist)
  107.         Canvas.Line -Step(Dist, 0)
  108.         Canvas.Line -Step(Dist, Dist)
  109.     Else
  110.         SierpC level - 1, Dist
  111.         Canvas.Line -Step(Dist, -Dist)
  112.         SierpD level - 1, Dist
  113.         Canvas.Line -Step(Dist, 0)
  114.         SierpB level - 1, Dist
  115.         Canvas.Line -Step(Dist, Dist)
  116.         SierpC level - 1, Dist
  117.     End If
  118. End Sub
  119. ' ************************************************
  120. ' Draw a type D sierpinski sub-curve.
  121. ' ************************************************
  122. Sub SierpD(level As Integer, Dist As Single)
  123.     If level = 1 Then
  124.         Canvas.Line -Step(-Dist, -Dist)
  125.         Canvas.Line -Step(0, -Dist)
  126.         Canvas.Line -Step(Dist, -Dist)
  127.     Else
  128.         SierpD level - 1, Dist
  129.         Canvas.Line -Step(-Dist, -Dist)
  130.         SierpA level - 1, Dist
  131.         Canvas.Line -Step(0, -Dist)
  132.         SierpC level - 1, Dist
  133.         Canvas.Line -Step(Dist, -Dist)
  134.         SierpD level - 1, Dist
  135.     End If
  136. End Sub
  137. ' ************************************************
  138. ' Draw a type A sierpinski sub-curve.
  139. ' ************************************************
  140. Sub SierpA(level As Integer, Dist As Single)
  141.     If level = 1 Then
  142.         Canvas.Line -Step(-Dist, Dist)
  143.         Canvas.Line -Step(-Dist, 0)
  144.         Canvas.Line -Step(-Dist, -Dist)
  145.     Else
  146.         SierpA level - 1, Dist
  147.         Canvas.Line -Step(-Dist, Dist)
  148.         SierpB level - 1, Dist
  149.         Canvas.Line -Step(-Dist, 0)
  150.         SierpD level - 1, Dist
  151.         Canvas.Line -Step(-Dist, -Dist)
  152.         SierpA level - 1, Dist
  153.     End If
  154. End Sub
  155. Sub GetParameters()
  156.     If Not IsNumeric(LevelText.Text) Then _
  157.         LevelText.Text = "5"
  158.     TheLevel = CInt(LevelText.Text)
  159.     ' Compute the side length for this level.
  160.     StartLength = TotalLength / (3 * 2 ^ TheLevel - 1)
  161.     ' Compute the upper left corner.
  162.     StartX = (Canvas.ScaleWidth - TotalLength) / 2
  163.     StartY = (Canvas.ScaleHeight - TotalLength) / 2 + _
  164.         StartLength
  165. End Sub
  166. Private Sub CmdGo_Click()
  167. Dim i As Integer
  168.     MousePointer = vbHourglass
  169.     DoEvents
  170.     ' Get the parameters.
  171.     GetParameters
  172.     ' Draw the curve.
  173.     Canvas.Cls
  174.     Canvas.CurrentX = StartX
  175.     Canvas.CurrentY = StartY
  176.     Sierpinski TheLevel, StartLength
  177.     MousePointer = vbDefault
  178. End Sub
  179. Private Sub Form_Resize()
  180.     Canvas.Move Canvas.Left, 0, _
  181.         ScaleWidth - Canvas.Left, ScaleHeight - 1
  182.     ' See how big we can make the curve.
  183.     If Canvas.ScaleHeight < Canvas.ScaleWidth Then
  184.         TotalLength = 0.9 * Canvas.ScaleHeight
  185.     Else
  186.         TotalLength = 0.9 * Canvas.ScaleWidth
  187.     End If
  188. End Sub
  189. Private Sub mnuFileExit_Click()
  190.     Unload Me
  191. End Sub
  192.